package cn.juni.controller;

import cn.juni.pojo.Role;
import cn.juni.pojo.UserInfo;
import cn.juni.service.IUserInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @author Juni
 * @date 2021/2/25
 */
@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    IUserInfoService userInfoService;

    /**
     * @RolesAllowed("ADMIN") 设置可以访问该请求地址的角色名称，此处设置的是只允许ADMIN的角色才可以访问
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "5") Integer pageSize,
                          Model model){
        List<UserInfo> userInfoList = userInfoService.findAll(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(userInfoList);
        model.addAttribute("pageInfo",pageInfo);
        return "user-list";
    }

    @RequestMapping("/save")
    public String saveUserInfo(UserInfo userInfo){
        userInfoService.insertUserInfo(userInfo);
        return "redirect:findAll";
    }

    @RequestMapping("/findUserInfo")
    public String findUserInfo(String id,Model model){
        UserInfo userInfo = userInfoService.findUserInfoById(id);
        model.addAttribute("userInfo",userInfo);
        return "user-show";
    }

    @RequestMapping("/findOtherRole")
    public String findOtherRole(String uid,Model model){
        List<Role> roleList = userInfoService.findOtherRole(uid);
        model.addAttribute("roleList",roleList);
        model.addAttribute("uid",uid);
        return "user-role-add";
    }

    @RequestMapping("/saveUserRole")
    public String saveUserRole(String userId,String[] ids){
        userInfoService.insertUserRole(userId,ids);
        return "redirect:findAll";
    }
}
